package com.niit.bc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.niit.bc.bean.BookBean;
import com.niit.bc.biz.BookBiz;
import com.niit.bc.biz.impl.BookBizImpl;
import com.niit.bc.util.web.RegularExpression;

public class AddBooks extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public AddBooks() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");

        SmartUpload upload = new SmartUpload();
        try {
            upload.initialize(this.getServletConfig(), request, response);
            upload.setMaxFileSize(1024 * 1024);
            upload.upload();

            com.jspsmart.upload.Request addBooks = upload.getRequest();
            String childTypeId = addBooks.getParameter("childTypeId");
            String bookName = addBooks.getParameter("bookName");
            String bookIsbn = addBooks.getParameter("bookISBN");
            String bookPrice = addBooks.getParameter("bookPrice");
            String bookAuthor = addBooks.getParameter("bookAuthor");
            String bookNum = addBooks.getParameter("bookNum");
            String bookPub = addBooks.getParameter("bookPub");
            String bookPubTime = addBooks.getParameter("bookPubTime");
            String bookPage = addBooks.getParameter("bookPage");
            String bookShowIndex = addBooks.getParameter("bookShowIndex");
            String bookDescribe = addBooks.getParameter("bookDescribe");
            String bookOther = addBooks.getParameter("bookOther");

            //�ж� bookNum��bookPrice��bookPage��bookShowIndex�ĸ�ʽ�Ƿ���ȷ
            boolean flag = false;
            boolean a = false;
            RegularExpression abr = new RegularExpression();
            flag = abr.numberRegx(bookNum);
            if (flag != true) {
                out.println("<script>alert('ͼ��������ʽ����');</script>");
                out.println("<script>history.back(-1)</script>");
                out.println("  </BODY>");
                out.println("</HTML>");
                out.flush();
                out.close();
                return;
            }
            a = abr.floatRegx(bookPrice);
            if (a != true) {
                out.println("<script>alert('ͼ��۸��ʽ����');</script>");
                out.println("<script>history.back(-1)</script>");
                out.println("  </BODY>");
                out.println("</HTML>");
                out.flush();
                out.close();
                return;
            }
            flag = abr.numberRegx(bookPage);
            if (flag != true) {
                out.println("<script>alert('ͼ��ҳ����ʽ����');</script>");
                out.println("<script>history.back(-1)</script>");
                out.println("  </BODY>");
                out.println("</HTML>");
                out.flush();
                out.close();
                return;
            }
            flag = abr.numberRegx(bookShowIndex);
            if (flag != true) {
                out.println("<script>alert('�Ƿ���ҳ��ʾ��ʽ����');</script>");
                out.println("<script>history.back(-1)</script>");
                out.println("  </BODY>");
                out.println("</HTML>");
                out.flush();
                out.close();
                return;
            }


            com.jspsmart.upload.File file = upload.getFiles().getFile(0);
            String name = file.getFileName();
            String fileName = "";

            String tail = name.substring(name.indexOf("."));
            String filePath = "uploadImages/";
            fileName = String.valueOf(new Date().getTime() + tail);
            fileName = filePath + fileName;

            file.saveAs(fileName, upload.SAVE_VIRTUAL);
            BookBean bookBean = new BookBean();
            bookBean.setBookAuthor(bookAuthor);
            bookBean.setBookDescribe(bookDescribe);
            bookBean.setBookIsbn(bookIsbn);
            bookBean.setBookName(bookName);
            bookBean.setBookNum(Integer.parseInt(bookNum));
            bookBean.setBookOther(bookOther);
            bookBean.setBookPage(Integer.parseInt(bookPage));
            bookBean.setBookPhoto(fileName);
            bookBean.setBookPrice(Double.parseDouble(bookPrice));
            bookBean.setBookPub(bookPub);
            bookBean.setBookPubTime(bookPubTime);
            bookBean.setBookShowIndex(Integer.parseInt(bookShowIndex));
            BookBiz book = new BookBizImpl();
            int n = book.addBooks(bookBean, Integer.parseInt(childTypeId));
            if (n > 0) {
                out.println("<script>alert('��ӳɹ�');</script>");
                out.println("<script>history.back(-1)</script>");
                out.println("  </BODY>");
                out.println("</HTML>");
                out.flush();
                out.close();
                return;
            }
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
